package pro.spring.controller.backController;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Set;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.util.NodeList;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import pro.spring.controller.backController.BaseController;
import pro.spring.model.Information;


@Controller("HtmlparserController")
@RequestMapping("/htmlparser")
public class HtmlparserController extends BaseController{


	private static String ENCODE="UTF-8";
	@SuppressWarnings("unused")
	private static void message(String szMsg){
		try{
			System.out.println(new String(szMsg.getBytes(ENCODE),System.getProperty("file.encoding")));
		}catch(Exception e){}
	}
	public static String opennFile(String szFileName){
		try{
			BufferedReader bis = new BufferedReader(new InputStreamReader(new FileInputStream(new File(szFileName)),ENCODE));
			String szContent="";
			String szTemp;
			while((szTemp=bis.readLine())!=null){
				szContent+=szTemp+"\n";
			}
			bis.close();
			return szContent;
		}catch(Exception e){
			e.printStackTrace();
			return "";
		}	
	}
	public static Information trueDo(Information info,String tag,String id,String tagName,String url)throws Exception{
		Information information=info;
		Parser parser = new Parser((HttpURLConnection)(new URL(url)).openConnection());
		NodeFilter filter = new HasAttributeFilter(tag,id);
		NodeList nodes = parser.extractAllNodesThatMatch(filter);
		if(nodes!=null){
			for(int i=0;i<nodes.size();i++){
				Node textnode = (Node) nodes.elementAt(i);
				if(tagName=="标题："){
					information.setInTitle(textnode.toPlainTextString());
				}else if(tagName=="正文："){
					//textnode.toPlainTextString()
					information.setInContent(textnode.toHtml());
				}
			}
		}
		return information;
	}
	@ResponseBody
	@RequestMapping(value="doparser.html",method=RequestMethod.POST)
	public String doparser(String urlZZ,String url,Integer type,String titletype,String titleid,String contenttype,String contentid){
		try{
			//http://blog.csdn.net/bluishglc/article/details/37807501
    		//http://news.sohu.com/[\\d]+/n[\\d]+.shtml
    	//http://tech.qq.com/a/20140716/034295.htm
    	//http://it.sohu.com/[\\d]+/n[\\d]+.shtml
    	//http://lamp68.blog.51cto.com/1946204/1439019
    	//http://blog.zol.com.cn/7083/article_7082287.html
			//urlZZ="http://it.sohu.com/[\\d]+/n[\\d]+.shtml";
			//url="http://it.sohu.com/";
			Set<String>links =LinkParser.getUrl(urlZZ,url);
			int i=0;
			for (String link : links) {
				Information information=new Information();
				information=trueDo(information,titletype,titleid,"标题：",link);
				information=trueDo(information,contenttype,contentid,"正文：",link);
				if(information!=null){
					information.setInType(type);
					htmlparserService.insert(information);
				}
			}
			if(urlZZ==""||url==""){
				return "false";
			}else{
				return "ok";
			}
		}catch(Exception e){
			e.printStackTrace();
			return "false";
		}
	}
}
